﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.IO.Compression;

namespace BlogHunter.Manager
{
    public class HTMLContentManager
    {
        /// <summary>
        /// GetHTMLContent
        /// </summary>
        /// <param name="targetUri"></param>
        /// <param name="encodeType"></param>
        /// <returns></returns>
        public static string GetHTMLContent(Uri targetUri, Encoding encodeType)
        {
            try
            {
                WebRequest http = HttpWebRequest.Create(targetUri);
                HttpWebResponse response = (HttpWebResponse)http.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), encodeType);

                string result = sr.ReadToEnd();

                sr.Close();
                response.Close();

                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return string.Empty;
            }
        }

        /// <summary>
        /// GetGZipEncodedHTMLContent
        /// </summary>
        /// <param name="targetUri"></param>
        /// <param name="encodeType"></param>
        /// <returns></returns>
        public static string GetGZipEncodedHTMLContent(Uri targetUri, Encoding encodeType)
        {
            try
            {
                HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(targetUri);
                HttpWebResponse wsp = (HttpWebResponse)wr.GetResponse();

                Stream st = wsp.GetResponseStream();
                if (wsp.ContentEncoding.ToLower().Contains("gzip"))
                {
                    st = new GZipStream(st, CompressionMode.Decompress);
                }
                StreamReader sr = new StreamReader(st, encodeType);

                string result = sr.ReadToEnd();

                sr.Close();
                st.Close();
                wsp.Close();

                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return string.Empty;
            }
        }

        /// <summary>
        /// Create HttpWebRequest for posting
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static HttpWebRequest CreatePostHttpWebRequest(string url)
        {
            HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
            webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10";
            webRequest.Accept = "*/*";
            webRequest.ContentType = "*/*";
            webRequest.Referer = "http://www.baidu.com";
            webRequest.CookieContainer = new CookieContainer();
            return webRequest;
        }
    }
}
